/***************************************************
 @Name :        小铭同学
 @Time :        2025-09-03
 @Email :       LiaoMingWJ233@163.com
 @Comment :     家电状态
 @FileName :    equipment_status.cpp
 @Version :     1.0
****************************************************/


#include "equipment_status.h"
#include "ui_equipment_status.h"

Equipment_Status::Equipment_Status(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Equipment_Status)
{
    ui->setupUi(this);
    
    key = new KeyDevice; // key1-key3状态
    
    tcp =  TCP::instance(); // 使用单例模式获取TCP实例
    
    io = new ioDevice; // 创建ioDevice对象

    // 创建定时器对象设置触发槽函数时间
    io_timer = new QTimer;

    my_timer = new QTimer;
        
    my_tcp = new QTimer;    
    
    
    connect(io_timer, &QTimer::timeout, this, &Equipment_Status::printData);
    
    io_timer->start(500);

    
    connect(tcp, &TCP::dataReceived, this, [=](const QString &data){
        tcp_data = data;
        if(tcp_data=="Key_user_k"){
            ui->label_key4->setPixmap(QPixmap(":/按钮_开"));
            tcp_data = "";        // 空字符串
        }
    });
    
    connect(my_tcp, &QTimer::timeout, this, [=](){
        if (tcp_data =="") {
            ui->label_key4->setPixmap(QPixmap(":/按钮_关"));
        }
    });
    
    my_tcp->start(1000);          
    
    connect(my_timer, &QTimer::timeout, this, &Equipment_Status::changeKey);
    
    my_timer->start(1000);
    
    //connect(my_timer, SIGNAL(timeout()), this, SLOT(changeKey()));    
    
    ui->label_zt->setText("家电状态");
    ui->label_zt->setStyleSheet("QLabel {"
        "    font-size: 50px;"           // 字体大小
        "    color: #87cefa;"            // 文字颜色
        "    font-weight: bold;"         // 字体粗细
        "    font-family: '楷体';"        // 字体家族
        "}"
    );
    
    
    ui->label_key1->setPixmap(QPixmap(":/按钮_关"));
    ui->label_key2->setPixmap(QPixmap(":/按钮_关"));
    ui->label_key3->setPixmap(QPixmap(":/按钮_关"));
    ui->label_key4->setPixmap(QPixmap(":/按钮_关"));
    
    ui->label_hy->setPixmap(QPixmap(":/火焰_关"));
    ui->label_rt->setPixmap(QPixmap(":/人体_关"));
    ui->label_gd->setPixmap(QPixmap(":/光电_关"));
    ui->label_dog->setPixmap(QPixmap(":/机械狗_关"));
    
}

Equipment_Status::~Equipment_Status()
{
    delete ui;
}

void Equipment_Status::label_dog_Status(QPixmap pixmap)
{
    ui->label_dog->setPixmap(pixmap);
}

void Equipment_Status::changeKey()
{
    int key1data = key->getKey1();
    int key2data = key->getKey2();
    int key3data = key->getKey3();

    if(!key1data){
        ui->label_key1->setPixmap(QPixmap(":/按钮_开"));
    }else{
        ui->label_key1->setPixmap(QPixmap(":/按钮_关"));
    }
    
    if(!key2data){
        ui->label_key2->setPixmap(QPixmap(":/按钮_开"));
    }else{
        ui->label_key2->setPixmap(QPixmap(":/按钮_关"));
    }
    
    if(!key3data){
        ui->label_key3->setPixmap(QPixmap(":/按钮_开"));
    }else{
        ui->label_key3->setPixmap(QPixmap(":/按钮_关"));
    }
}

void Equipment_Status::printData()
{
    int frameStatus = io->getFrame();
    int infreadStatus = io->getInfread();
    int photoswitchStatus = io->getPhotowitch();

    if (frameStatus == 1) {
        ui->label_hy->setPixmap(QPixmap(":/火焰_关"));
    }
    else{
        ui->label_hy->setPixmap(QPixmap(":/火焰_开"));
    }

    if (infreadStatus == 1) {
        ui->label_rt->setPixmap(QPixmap(":/人体_关"));
    }
    else{
        ui->label_rt->setPixmap(QPixmap(":/人体_开"));
    }

    if (photoswitchStatus == 1) {
        ui->label_gd->setPixmap(QPixmap(":/光电_开"));
    }
    else{                
        ui->label_gd->setPixmap(QPixmap(":/光电_关"));
    }
}

